#ifndef PACKETQUEUE_H
#define PACKETQUEUE_H

#include <vector>
#include "Mutex.h"

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/avutil.h"
}

using namespace std;

extern AVPacket gFlushPacket;

// av_init_packet(&flush_pkt);
// flush_pkt.data = (uint8_t *)&flush_pkt;

typedef struct Packet {
	AVPacket pkt;
	int serial;
}Packet;

class PacketQueue {
public:
	PacketQueue();
	~PacketQueue();

	void add(AVPacket &pkt);
	void addFlushPkt();
	bool isFlushPkt(AVPacket &pkt);
	int get(AVPacket &pkt, int &serial);
	bool isEmpty(){return m_Queue.size() == 0 ? true : false;};
	int size(){return m_Size;};
	int nb_pkt(){return m_NBPackets;}
	int64_t duration(){return m_Duration;}
	void wait(){
		AutoLock lock(&m_Cond); 
		m_Cond.wait();
	}
	void notify(){m_Cond.notify();}
	void clear() {
		m_Queue.clear();
		m_NBPackets = 0;
		m_Size = 0;
		m_Duration = 0;
	}
	void abort() {
		m_AbortRequest = true;
		notify();
	}

	bool isAbort(){return m_AbortRequest;}
	static bool m_FlashPktInit;
	int m_Serial;
protected:
	Condition m_Cond;
	vector<Packet> m_Queue;
	int m_NBPackets;
	int m_Size;
	int64_t m_Duration;
	bool m_AbortRequest;
	
	int recycle_count;
	int alloc_count;
	int is_buffer_indicator;
};

#endif